﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Entity;
namespace VIETPHONG
{
    class ChucNang
    {
        private static VIETPHONGEntities db = new VIETPHONGEntities();
        public static KHACHHANG InsertKhachHang(KHACHHANG khachhang)
        {
            try
            {                
                db.KHACHHANGs.Add(khachhang);
                db.SaveChanges();                
                return khachhang;
            }
            catch (Exception ex) { throw; }
        }
        public static KHACHHANG CapNhatKhachHang(KHACHHANG khachhang)
        {
            try
            {
                  db.Entry(khachhang).State = EntityState.Modified;
                  db.SaveChanges();
                  return khachhang;
            }
            catch (Exception ex) { throw; }
        }
        public static KHACHHANG XoaKhachHang(KHACHHANG khachhang)
        {
            try
            {
                db.Entry(khachhang).State = EntityState.Deleted;
                db.SaveChanges();
                return khachhang;
            }
            catch (Exception ex) { throw; }
        }
        public static SANPHAM ThemSanPham(SANPHAM sanpham)
        {
            try
            {
                db.SANPHAMs.Add(sanpham);
                db.SaveChanges();
                return sanpham;
            }
            catch (Exception ex) { throw; }
        }
        public static SANPHAM CapNhatSanPham(SANPHAM sanpham)
        {
            try
            {
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return sanpham;
            }
            catch (Exception ex) { throw; }
        }
        public static SANPHAM XoaSanPham(SANPHAM sanpham)
        {
            try
            {
                db.Entry(sanpham).State = EntityState.Deleted;
                db.SaveChanges();
                return sanpham;
            }
            catch (Exception ex) { throw; }
        }
        public static HOADON ThemHoaDon(HOADON hoadon)
        {
            try
            {
                db.HOADONs.Add(hoadon);
                db.SaveChanges();
                return hoadon;
            }
            catch (Exception ex) { throw; }
        }

        public static HOADON CapNhatHoaDon(HOADON hoadon)
        {
            try
            {
                db.Entry(hoadon).State = EntityState.Modified;
                db.SaveChanges();
                return hoadon;
            }
            catch (Exception ex) { throw; }
        }

        public static HOADON XoaHoaDon(HOADON hoadon)
        {
            try
            {
                db.Entry(hoadon).State = EntityState.Deleted;
                db.SaveChanges();
                return hoadon;
            }
            catch (Exception ex) { throw; }
        }

        public static CHITIETHOADON ThemChiTietHoaDon(CHITIETHOADON chitiet)
        {
            try
            {
                db.CHITIETHOADONs.Add(chitiet);
                db.SaveChanges();
                return chitiet;
            }
            catch (Exception) { throw; }           
        }
        public static CHITIETHOADON CapNhatChiTietHoaDon(CHITIETHOADON chitiet)
        {
            try
            {
                db.Entry(chitiet).State = EntityState.Modified;
                db.SaveChanges();
                return chitiet;
            }
            catch (Exception) { throw; }
        }
        public static CHITIETHOADON XoaChiTietHoaDon(CHITIETHOADON chitiet)
        {
            try
            {
                db.Entry(chitiet).State = EntityState.Deleted;
                db.SaveChanges();
                return chitiet;
            }
            catch (Exception) { throw; }
        }
        public static bool IsNumber(string pText)
        {
            //Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            //return regex.IsMatch(pText);
            foreach (Char c in pText)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }

        public static string DocSoThanhChu(string number)//<=
        {
            string strReturn = "";
            string s = number;
            while (s.Length > 0 && s.Substring(0, 1) == "0")
            {
                s = s.Substring(1);
            }
            string[] so = new string[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
            string[] hang = new string[] { "", "nghìn", "triệu", "tỷ" };
            int i, j, donvi, chuc, tram;

            bool booAm = false;
            decimal decS = 0;

            try
            {
                decS = Convert.ToInt64(s.ToString());
            }
            catch { }

            if (decS < 0)
            {
                decS = -decS;
                //s = decS.ToString();
                booAm = true;
            }
            i = s.Length;
            if (i == 0)
                strReturn = so[0] + strReturn;
            else
            {
                j = 0;
                while (i > 0)
                {
                    donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                    i--;
                    if (i > 0)
                        chuc = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        chuc = -1;
                    i--;
                    if (i > 0)
                        tram = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        tram = -1;
                    i--;
                    if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                        strReturn = hang[j] + strReturn;
                    j++;
                    if (j > 3) j = 1;   //Tránh lỗi, nếu dưới 13 số thì không có vấn đề.
                    //Hàm này chỉ dùng để đọc đến 9 số nên không phải bận tâm
                    if ((donvi == 1) && (chuc > 1))
                        strReturn = "mốt " + strReturn;
                    else
                    {
                        if ((donvi == 5) && (chuc > 0))
                            strReturn = "năm " + strReturn;
                        else if (donvi > 0)
                            strReturn = so[donvi] + " " + strReturn;
                    }
                    if (chuc < 0) break;//Hết số
                    else
                    {
                        if ((chuc == 0) && (donvi > 0)) strReturn = "lẻ " + strReturn;
                        if (chuc == 1) strReturn = "mười " + strReturn;
                        if (chuc > 1) strReturn = so[chuc] + " mươi " + strReturn;
                    }
                    if (tram < 0) break;//Hết số
                    else
                    {
                        if ((tram > 0) || (chuc > 0) || (donvi > 0)) strReturn = so[tram] + " trăm " + strReturn;
                    }
                    strReturn = " " + strReturn;
                }
            }
            if (booAm) strReturn = "Âm " + strReturn;
            return strReturn.Trim();// = str+ "đồng chẵn";
        }
    }
}
